package manager

import com.hontech.kdxclient.R
import ext.log

object PayManager {
    const val PAY_WECHAT = "wechat"
    const val PAY_ALIPAY = "alipay"

    var PayType = PAY_WECHAT
    var CurrentOrder = ""

    fun refund(order: String, goodsType: String, mark: String) {
        TaskManager.DelayHandler.post(RefundTask(order, goodsType, mark))
    }

    fun refundAll(order: String) {
        TaskManager.DelayHandler.post(RefundAllTask(order))
    }

    private class RefundAllTask(val order: String) : Runnable {

        private fun exec() {
            HttpRequestManager.refundWithAlipay(order, "收不到支付推送退款", "收不到支付推送退款")
            HttpRequestManager.refundWithWechat(order, "收不到支付推送退款", "收不到支付推送退款")
        }

        override fun run() {
            try {
                exec()
            } catch (e: Exception) {
                e.printStackTrace()
                TaskManager.DelayHandler.postDelayed(this, 5 * 1000)
            }
        }
    }

    private class RefundTask(val order: String, val goodsType: String, val mark: String) : Runnable {

        override fun run() {
            log("开始退款:$order $goodsType $mark", "退款")
            try {
                HttpRequestManager.refundWithAlipay(order, mark, goodsType)
                HttpRequestManager.refundWithWechat(order, mark, goodsType)
            } catch (e: Exception) {
                e.printStackTrace()
                TaskManager.DelayHandler.postDelayed(this, 1000)
            }
        }
    }
}
